package fresher.java.finaltest.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;

import fresher.java.finaltest.entity.Bee;
import fresher.java.finaltest.entity.Bees;
import fresher.java.finaltest.entity.Drone;
import fresher.java.finaltest.entity.Queen;
import fresher.java.finaltest.entity.Worker;
import fresher.java.finaltest.util.ConnectDB;

public class BeeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List<Bees> beeList;
	private List<Bees> beesTop10Random;
	private Connection conn;
	private PreparedStatement preStatement;

	private Bees bess;

	@Override
	public String execute() throws Exception {
		if(saveBees(bess)>0){
		return INPUT;
		}
		return ERROR;
	}

	public String getData() {
		beeList = getBeesList();
		return SUCCESS;
	}

	public String getDataRandom() {
		beesTop10Random = getTopBeesRandom();
		return SUCCESS;
	}

	public List<Bees> getBeesList() {
		String sql = "select * from Bees";
		List<Bees> list = new LinkedList<>();
		Bees bees = null;
		try {
			conn = ConnectDB.getConnect();
			preStatement = conn.prepareStatement(sql);
			ResultSet rs = preStatement.executeQuery();
			while (rs.next()) {
				bees = new Bees(rs.getInt("id"), rs.getString("BeesType"),
						rs.getString("BeesName"), rs.getFloat("BeesHeath"));
				list.add(bees);
			}
			return list;
		} catch (Exception e) {
			return null;
		} finally {
			try {
				closeResource();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void closeResource() throws SQLException {
		if (conn != null)
			conn.close();
		if (preStatement != null)
			preStatement.close();
	}

	// Getter and Setter
	public List<Bees> getBeeList() {

		return beeList;
	}

	public void setBeeList(List<Bees> beeList) {
		this.beeList = beeList;
	}

	public List<Bees> getBeesTop10Random() {
		return beesTop10Random;
	}

	public void setBeesTop10Random(List<Bees> beesTop10Random) {
		this.beesTop10Random = beesTop10Random;
	}

	public Bees getBess() {
		return bess;
	}

	public void setBess(Bees bess) {
		this.bess = bess;
	}

	// Function
	public List<Bees> getTopBeesRandom() {

		Bees bees = null;
		List<Bee> list = new LinkedList<>();
		list.add(new Worker());
		list.add(new Queen());
		list.add(new Drone());

		List<Bees> beesList = new LinkedList<>();
		for (int i = 0; i < 10; i++) {
			bees = new Bees();
			int type = (int) ((Math.random()) * list.size());
			if (type == 0)
				type = 3;
			if (type == 1) {
				bees.setBeeName(" Bee Worker" + i);
				bees.setBeeType(list.get(0).name);
				bees.setBeeHeath(100);
				beesList.add(bees);
			}
			if (type == 2) {
				bees.setBeeName(" Bee Queen" + i);
				bees.setBeeType(list.get(1).name);
				bees.setBeeHeath(100);
				beesList.add(bees);
			}
			if (type == 3) {
				bees.setBeeName(" Bee Drone" + i);
				bees.setBeeType(list.get(2).name);
				bees.setBeeHeath(100);
				beesList.add(bees);
			}
		}

		return beesList;

	}

	public int saveBees(Bees bees) {
		String sql = "insert into Bees(BeesType,BeesName,BeesHeath) values (?,?,?)";
		try {
			// closeResource();
			conn = ConnectDB.getConnect();
			preStatement = conn.prepareStatement(sql);
			preStatement.setString(1,bees.getBeeName());
			preStatement.setString(2,bees.getBeeType());
			preStatement.setFloat(3,bees.getBeeHeath());
			return preStatement.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Co Loi Xay Ra");
			e.printStackTrace();
			return -1;
		} finally {
			try {
				closeResource();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
}
